package ui;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JToggleButton;
import javax.swing.ListSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import core.Preferences;

@SuppressWarnings("serial")
public class PreferencesPanel extends JPanel implements ActionListener {
	
	private static final String[] titles = 
			{"Presentation", "  Font", "  Color"};
	
	private Preferences prefs;
	private JCheckBox debugCb;
	private JComboBox monitorSelect;
	private FontChooser cjkfc, mainfc;
	private JToggleButton leftBtn, centerBtn, rightBtn;
	private JPanel cardPanel;
	private JPanel[] cards;
	private JColorChooser bgc, fgc;
	
	public PreferencesPanel(Preferences p) {
		super(new GridBagLayout());
		setName("Preferences");
		prefs = p;
		initComponents();
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equalsIgnoreCase("save")) {
			save();
		}
		else if (e.getActionCommand().equalsIgnoreCase("close")) {
		}
	}
	
	public void save() {
		prefs.setDefaultMonitor(monitorSelect.getSelectedIndex());
		prefs.debugMode = debugCb.isSelected();
		prefs.setCJKFont(cjkfc.getSelectedFont());
		prefs.setMainFont(mainfc.getSelectedFont());
		if (leftBtn.isSelected())
			prefs.setAlign(ScreenDisplay.LEFT);
		else if (centerBtn.isSelected())
			prefs.setAlign(ScreenDisplay.CENTER);
		else if (rightBtn.isSelected())
			prefs.setAlign(ScreenDisplay.RIGHT);
		prefs.setBgColor(bgc.getColor());
		prefs.setFgColor(fgc.getColor());
		prefs.save();
	}
	
	private void initComponents() {
		cardPanel = new JPanel(new CardLayout());
		cardPanel.setVisible(true);
		cards = new JPanel[titles.length];
		
		GridBagConstraints c;
		
		// Presentation card
		{
			cards[0] = new JPanel(new GridBagLayout());
			cards[0].setVisible(true);
			c = new GridBagConstraints();
			
			c.anchor = GridBagConstraints.NORTHWEST;
			c.gridy = 0;
			debugCb = new JCheckBox();
			debugCb.setSelected(prefs.debugMode);
			cards[0].add(new JLabel("Test mode (quarter-sized display):   "), c);
			c.weightx = 1;
			cards[0].add(debugCb, c);
	
			c.gridy++;
			String[] strs = new String[Preferences.gcs.length];
			for (int i=0; i<Preferences.gcs.length; i++)
				strs[i] = "Monitor " + i;
			monitorSelect = new JComboBox(strs);
			monitorSelect.setSelectedIndex(prefs.getDefaultMonitor());
			c.weightx = 0;
			cards[0].add(new JLabel("Presentation monitor:"), c);
			c.gridwidth = GridBagConstraints.REMAINDER;
			c.weightx = 1;
			cards[0].add(monitorSelect, c);
		}
		
		
		// Font panel
		{
			cards[1] = new JPanel(new GridBagLayout());
			cards[1].setVisible(true);
			c = new GridBagConstraints();
			
			c.anchor = GridBagConstraints.NORTHWEST;
			c.gridy = 0;
			cards[1].add(new JLabel("Main font:"), c);
			c.gridy++;
			mainfc = new FontChooser();
			mainfc.setSelectedFont(prefs.getMainFont());
			JLabel mainPreview = new JLabel("Preview");
			mainfc.setPreview(mainPreview);
			cards[1].add(mainfc, c);
			c.gridy++;
			JPanel mpPanel = new JPanel();
			mpPanel.add(mainPreview);
			mpPanel.setPreferredSize(new Dimension(500, 120));
			mpPanel.setBorder(BorderFactory.createTitledBorder("Preview"));
			mpPanel.setVisible(true);
			cards[1].add(mpPanel, c);
			
			c.gridy++;
			cards[1].add(new JLabel("CJK font:"), c);
			c.gridy++;
			cjkfc = new FontChooser();
			cjkfc.setSelectedFont(prefs.getCJKFont());
			JLabel cjkPreview = new JLabel("\u6f22\u5b57");
			cjkfc.setPreview(cjkPreview);
			cards[1].add(cjkfc, c);
			c.gridy++;
			JPanel cpPanel = new JPanel();
			cpPanel.add(cjkPreview);
			cpPanel.setPreferredSize(new Dimension(500, 120));
			cpPanel.setBorder(BorderFactory.createTitledBorder("Preview"));
			cpPanel.setVisible(true);
			cards[1].add(cpPanel, c);
			
			c.gridy++;
			cards[1].add(new JLabel("Text alignment:"), c);
			c.gridy++;
			ButtonGroup group = new ButtonGroup();
			leftBtn = new JToggleButton(new ImageIcon(
					Presenter.class.getResource("/res/sm/format-justify-left.png")));
			centerBtn = new JToggleButton(new ImageIcon(
							Presenter.class.getResource("/res/sm/format-justify-center.png")));
			rightBtn = new JToggleButton(new ImageIcon(
							Presenter.class.getResource("/res/sm/format-justify-right.png")));
			group.add(leftBtn);
			group.add(centerBtn);
			group.add(rightBtn);
			if (prefs.getAlign() == ScreenDisplay.LEFT)
				leftBtn.setSelected(true);
			else if (prefs.getAlign() == ScreenDisplay.CENTER)
				centerBtn.setSelected(true);
			else if (prefs.getAlign() == ScreenDisplay.RIGHT)
				rightBtn.setSelected(true);
			JPanel groupPanel = new JPanel();
			groupPanel.add(leftBtn);
			groupPanel.add(centerBtn);
			groupPanel.add(rightBtn);
			cards[1].add(groupPanel, c);
		}
		
		// Color panel
		{
			cards[2] = new JPanel(new GridBagLayout());
			c = new GridBagConstraints();
			c.gridy = 0;
			c.fill = GridBagConstraints.HORIZONTAL;
			cards[2].add(new JLabel("Background color:"), c);
			cards[2].add(new JLabel("Text color:"), c);
			
			c.gridy++;
			bgc = new JColorChooser(prefs.getBgColor());
			cards[2].add(bgc, c);
			fgc = new JColorChooser(prefs.getFgColor());
			
			JPanel colorpPanel = new JPanel();
			colorpPanel.setPreferredSize(new Dimension(500, 120));
			final JLabel colorPreview = new JLabel("Preview", JLabel.CENTER);
			colorPreview.setOpaque(true);
			colorPreview.setFont(new java.awt.Font("Dialog", java.awt.Font.PLAIN, 42));
			colorPreview.setPreferredSize(colorpPanel.getPreferredSize());
			colorpPanel.add(colorPreview);
			
			ChangeListener cl = new ChangeListener() {
				public void stateChanged(ChangeEvent e) {
					colorPreview.setBackground(bgc.getColor());
					colorPreview.setForeground(fgc.getColor());
				}
			};
			bgc.getSelectionModel().addChangeListener(cl);
			fgc.getSelectionModel().addChangeListener(cl);
			bgc.setPreviewPanel(colorpPanel);
			fgc.setPreviewPanel(colorpPanel);
			cards[2].add(fgc, c);
			
			// give colorPreview the initial color values
			cl.stateChanged(null);
			
			c.gridy++;
			c.gridwidth = GridBagConstraints.REMAINDER;
			cards[2].add(colorpPanel, c);
		}
		
		
		// left panel
		final JList list = new JList(titles);
		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		list.setBorder(BorderFactory.createCompoundBorder(
				BorderFactory.createLineBorder(java.awt.Color.black),
				BorderFactory.createEmptyBorder(4, 0, 4, 0)));
		list.addListSelectionListener(new ListSelectionListener() {
			public void valueChanged(ListSelectionEvent e) {
				CardLayout cl = (CardLayout)cardPanel.getLayout();
				cl.show(cardPanel, (String)list.getSelectedValue());
			}
		});
		list.setSelectedIndex(0);
		
		// main panel
		c = new GridBagConstraints();
		c.gridx = 0;
		c.gridy = 0;
		c.fill = GridBagConstraints.VERTICAL;
		c.anchor = GridBagConstraints.WEST;
		c.weighty = .1;
		for (int i=0; i<cards.length; i++) {
//			cards[i].setBorder(BorderFactory.createTitledBorder(titles[i]));
			cardPanel.add(cards[i], titles[i]);
		}
		add(list, c);
		
		c.fill = GridBagConstraints.BOTH;
		c.weightx = 1;
		c.gridx++;
		c.insets.left = 7;
		add(cardPanel, c);
		
//		c.gridx = 0;
//		c.gridy = 1;
//		c.weighty = 0;
//		c.fill = GridBagConstraints.NONE;
//		JButton saveBtn = new JButton("Save");
//		saveBtn.setActionCommand("save");
//		saveBtn.addActionListener(this);
//		add(saveBtn, c);
		
		setVisible(true);
	}
	
	public static void main(String[] args) {
		JFrame fr = new JFrame();
		fr.add(new PreferencesPanel(new Preferences()));
		fr.pack();
		fr.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		fr.setVisible(true);
	}
}
